<?php

namespace app\admin\controller\subject;

// 引入基类控制器
use app\common\controller\Backend;

// 课程分类
class Order extends Backend
{
  // 设置关联查询
  protected $relationSearch = true;

  public function __construct()
  {
    parent::__construct();

    $this->model = model('Subject.Order');
  }

  public function index()
  {
    if ($this->request->isAjax()) {
      // 过滤请求参数中的html标签和取消两边你空白
      $this->request->filter(['strip_tags', 'trim']);
      // 获取查询参数
      list($where, $sort, $order, $offset, $limit) = $this->buildparams();

      $total = $this->model
        ->with(['subject', 'business'])
        ->where($where)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->count();

      $list = $this->model
        ->with(['subject', 'business'])
        ->where($where)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();

      $result = [
        'rows' => $list,
        'total' => $total
      ];

      return json($result);
    }

    return $this->view->fetch();
  }

  public function del($ids = NULL)
  {
    $list = $this->model->select($ids);

    if (!$list) {
      $this->error(__('No Results were found'));
      exit;
    }

    $result = $this->model->destroy($ids);

    if ($result) {
      $this->success('删除成功');
      exit;
    } else {
      $this->error($this->model->getError());
      exit;
    }
  }
}
